% approx angular range...
aperture=2.8;%mm
path_length=100;%mm
da=2*atan(aperture/2/path_length);

alpha=linspace(-da,da,100);

n=1.51680;
t=5; %mm

beta=asin(sin(alpha)/n);
w=t*(sin(alpha)-cos(alpha).*tan(beta));
plot(alpha/pi,beta/pi);
plot(alpha/pi,w);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% zoom lens plot

p1 = 120; %mm

lens_d    = 6;   % mm
obj_d     = 2.8; % mm
initial_d = 4;   % mm
for d0=linspace(f1-0.25,f1+0.25,20)
%d0        = 9.9;  % mm
d1        = 0;  % mm

d2        = p1-d0;  % mm
d3        = 4;  % mm
f1        = 10; % mm
f2        = Inf; % mm
f3        = 2;   % mm

na=0.22;
points = [0; asin(na)];
z=0;
clf
a = [];

% d0 of free space
mat=[1 d0; 0 1];
points = [points mat*points(:,end)];
z = [z z(end)+d0];

% first lens
mat=[1 0; -1/f1 1];
points = [points mat*points(:,end)];
z = [z z(end)];
a(end+1)=line(z(end)*[1 1],lens_d/2*[1 -1]);

% d1 of free space
mat=[1 d1; 0 1];
points = [points mat*points(:,end)];
z = [z z(end)+d1];

% second lens
mat=[1 0; -1/f2 1];
points = [points mat*points(:,end)];
z = [z z(end)];
a(end+1)=line(z(end)*[1 1],lens_d/2*[1 -1]);

% d2 of free space
mat=[1 d2; 0 1];
points = [points mat*points(:,end)];
z = [z z(end)+d2];

% third (objective) lens
mat=[1 0; -1/f3 1];
points = [points mat*points(:,end)];
z = [z z(end)];
a(end+1)=line(z(end)*[1 1],obj_d/2*[1 -1]);
fl=line((f3+z(end))*[1 1],lens_d/2*[1 -1]);
set(fl,'linestyle','--','color','k')

% f3 of free space
mat=[1 f3; 0 1];
points = [points mat*points(:,end)];
z = [z z(end)+f3];
spotsize=abs(2*points(1,end));

% another f3 of free space
mat=[1 f3; 0 1];
points = [points mat*points(:,end)];
z = [z z(end)+f3];


l(1)=line(z,points(1,:));
l(2)=line(z,-points(1,:));

a(end+1)=line([min(z), max(z)],[0 0]);
set(a,'color','k')
%axis equal
ylim([-3 3]);
set(l,'marker','x')
pause(0.25);
disp(sprintf('spotsize = %gum',spotsize*1000));
end

